package com.heima.wemedia.service.impl;

import com.heima.common.constants.ScheduleConstants;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.schedule.dtos.Task;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.schedule.feign.TaskFeign;
import com.heima.utils.common.JsonUtils;
import com.heima.wemedia.service.WmNewTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 定时发布文章
 */
@Service
@Slf4j
public class WmNewTaskServiceImpl implements WmNewTaskService {

    @Autowired
    private TaskFeign taskFeign;

    @Override
    public void addTaskToList(WmNews wmNews) {
        Task task = new Task();
        task.setTaskTopic(ScheduleConstants.TASK_TOPIC_NEWS_PUBLISH);
        task.setExecuteTime(wmNews.getPublishTime().getTime());

        WmNews news = new WmNews();
        news.setId(wmNews.getId());
        task.setParameters(JsonUtils.toString(news));//任务执行参数

        ResponseResult<Long> responseResult = taskFeign.addTask(task);
        if (responseResult.getCode().equals(200)) {
            Long taskId = responseResult.getData();
            //把taskId存入wm_news的taskId
            log.info("文章已加入定时列表中。文章ID：{},任务ID：{}",wmNews.getId(),taskId);
        }
    }
}
